﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using MpSoft.Library.DTO;
using MpSoft.Basic.DataAccess;

namespace MpSoft.Library.DAO.Contents
{
    /// <summary>
    /// This object represents the methods of a Adv.
    /// </summary>
    internal class NewsLanguageDAO
    {

        #region general

        private static NewsLanguage LoadObj(IDataReader datareader)
        {
            NewsLanguage obj = new NewsLanguage();
            obj.NLID = MpDataHelp.GetInt(datareader, "NLID");
            obj.NewsID = MpDataHelp.GetInt(datareader, "NewsID");
            obj.LanguageCode = MpDataHelp.GetString(datareader, "LanguageCode");
            obj.Title = MpDataHelp.GetString(datareader, "Title");
            obj.Description = MpDataHelp.GetString(datareader, "Description");
            obj.Content = MpDataHelp.GetString(datareader, "Content");
            obj.MetaTitle = MpDataHelp.GetString(datareader, "MetaTitle");
            obj.MetaKeyword = MpDataHelp.GetString(datareader, "MetaKeyword");
            obj.MetaDescription = MpDataHelp.GetString(datareader, "MetaDescription");
            obj.SeName = MpDataHelp.GetString(datareader, "SeName");
            obj.FormatUrl = MpDataHelp.GetString(datareader, "FormatUrl");
            return obj;
        }

        #endregion

        public static int NewsLanguage_Add(NewsLanguage obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_NewsLanguage_Add");
            db.AddOutParameter(dbCommand, "NLID", DbType.Int32, obj.NLID);
            db.AddInParameter(dbCommand, "NewsID", DbType.Int32, obj.NewsID);
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, obj.LanguageCode);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Content", DbType.String, obj.Content);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "FormatUrl", DbType.String, obj.FormatUrl);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return Convert.ToInt32(db.GetParameterValue(dbCommand, "@NLID"));
            return -1;
        }
        public static int NewsLanguage_Update(NewsLanguage obj)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_NewsLanguage_Update");
            db.AddInParameter(dbCommand, "NLID", DbType.Int32, obj.NLID);
            db.AddInParameter(dbCommand, "NewsID", DbType.Int32, obj.NewsID);
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, obj.LanguageCode);
            db.AddInParameter(dbCommand, "Title", DbType.String, obj.Title);
            db.AddInParameter(dbCommand, "Description", DbType.String, obj.Description);
            db.AddInParameter(dbCommand, "Content", DbType.String, obj.Content);
            db.AddInParameter(dbCommand, "MetaTitle", DbType.String, obj.MetaTitle);
            db.AddInParameter(dbCommand, "MetaKeyword", DbType.String, obj.MetaKeyword);
            db.AddInParameter(dbCommand, "MetaDescription", DbType.String, obj.MetaDescription);
            db.AddInParameter(dbCommand, "SeName", DbType.String, obj.SeName);
            db.AddInParameter(dbCommand, "FormatUrl", DbType.String, obj.FormatUrl);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static int NewsLanguage_Delete(int NLID)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_NewsLanguage_Delete");
            db.AddInParameter(dbCommand, "NLID", DbType.Int32, NLID);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return 1;
            return -1;
        }

        public static List<NewsLanguage> NewsLanguage_Select(int NLID, int NewsID, string LanguageCode)
        {
            Database db = MpDataHelp.CreateConnection();
            DbCommand dbCommand = db.GetStoredProcCommand("Mp_NewsLanguage_Select");
            db.AddInParameter(dbCommand, "NLID", DbType.Int32, NLID);
            db.AddInParameter(dbCommand, "NewsID", DbType.Int32, NewsID);
            db.AddInParameter(dbCommand, "LanguageCode", DbType.String, LanguageCode);
            List<NewsLanguage> listObj = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    (listObj = new List<NewsLanguage>()).Add(LoadObj(dataReader));
                    while (dataReader.Read())
                        listObj.Add(LoadObj(dataReader));
                }
            }
            return listObj;
        }
    }
}
